﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace SMS_Gateway
{
    abstract public class Gateway
    {
        public abstract bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode);

        public abstract bool IsConnected { get; }
    }
    abstract public class HTTPGateway:Gateway
    {
        protected string BuildQueryString(string prefix, KeyValuePair<string, string>[] fields)
        {
            StringBuilder queryStringBuilder= new StringBuilder();
            queryStringBuilder.Append(prefix);
            queryStringBuilder.Append('?');

            string[] fieldequalvalueStringArr= (from x in fields let newS=(x.Key+"="+x.Value) select newS).ToArray();

            queryStringBuilder.Append(String.Join("&",fieldequalvalueStringArr));
            
            //for
            //queryStringBuilder.Append

            return queryStringBuilder.ToString();

        }

        protected string HTTPSend(string sHTTPInputData)
        {

            string sResult;                         //-- the result to be returned from this function
            HttpWebResponse httpResponse = null;    //-- the response buffer used after the Request (HTTP GET) operation


            try
            {

                //-- create the HTTP GET request
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(sHTTPInputData);

                //-- now get the response
                httpResponse = (HttpWebResponse)(httpReq.GetResponse());
                StreamReader input = new StreamReader(httpResponse.GetResponseStream());
                sResult = input.ReadToEnd();
            }

            catch (Exception e)
            {
                //-- something went wrong. Return result.
                sResult = "HTTP Error: " + e.Message;
            }

            finally
            {
                //-- clean up
                if (httpResponse != null)
                    httpResponse.Close();
            }

            //-- return result
            return sResult;
        }

 
        public abstract override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode);

        public override abstract bool IsConnected { get; }
    }
    abstract public class SOAPGateway : Gateway
    {
        public abstract void LogOut();


        public abstract override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode);

        public override abstract bool IsConnected { get; }
    }

}
